
package com.csz.model;

import com.csz.dao.Persistencia;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/* @author Arthur F. Zanona */
public class Usuario extends Persistencia {
    private String login;
    private String pass;
    private Integer pesId ;

    public Usuario() {
    }

    public Usuario(Integer id, String login, String pass , Integer pesId) {
        this.id = id;
        this.login = login;
        this.pass = pass;
        this.pesId = pesId;
    }

    public String getLogin() {
        return login;
    }

    public Boolean isValido(String pass) {
        return pass.equals(this.pass);
    }

    @Override
    public void salvar() {
        String namedQuery = this.getId() == null ? Inserir : Atualizar ;
        List<Object> params = Arrays.asList( new Object[] { this.login , this.pass } );
        if ( this.getId() != null ) params.add( this.getId() );
        PreparedStatement ps = createStatementNamedQuery(namedQuery, params);
        try {
            ps.execute();
        } catch (SQLException ex) {}
    }

   
    public static Object consultar(Integer id) {
        return Usuario.consultarNamedQuery(ConsultarPorId, Arrays.asList(new Object[]{id}));
    }

    @Override
    public void remover() {
        PreparedStatement ps = createStatementNamedQuery(Remover, Arrays.asList( new Object[] { this.getId() } ));
        try {
            ps.execute();
        } catch (SQLException ex) {}
    }

    public static List<Object> consultar(){
        return Usuario.consultarNamedQuery(ConsultarTodos, Arrays.asList(new Object[]{}));
    }
    
    public static List<Object> consultarNamedQuery(String namedQuery, List params){
        PreparedStatement ps = createStatementNamedQuery(namedQuery, params);
        List<Object> lista = new ArrayList<Object>();
        try {
            ResultSet rs = ps.executeQuery();
            while ( rs.next() ) {
                Usuario u = new Usuario( rs.getInt("usu_id")  , rs.getString("usu_login") , rs.getString("usu_pass") , rs.getInt("usu_pes_id"));
                lista.add(u);
            }
        }
        catch (SQLException ex) {}
        return lista;
    }

    // QUERY SQL --------------------------------------------------------------------------------------------------------------
    public static final String ConsultarPorLogin  = "SELECT usu_id,usu_login,usu_pass,usu_pes_id FROM usuario WHERE usu_login = ? and usu_pass = ?";
    public static final String ConsultarTodos  = "SELECT usu_id,usu_login,usu_pass,usu_pes_id FROM usuario";
    public static final String ConsultarPorId = "SELECT * FROM .. WHERE .._id = ?";
    private static final String Inserir = "INSERT INTO USUARIO (usu_login,usu_pass,usu_pes_id) VALUES (?,?,?,?)";
    private static final String Remover = "DELETE FROM USUARIO WHERE usu_id = ?";
    private static final String Atualizar = "UPDATE USUARIO SET usu_login = ? , usu_pass = ? WHERE usu_id = ?";
    // FIM QUERY SQL --------------------------------------------------------------------------------------------------------------
}
